package com.kxf.androidtestdemo.view

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.LinearLayout
import androidx.annotation.RequiresApi
import com.kxf.androidtestdemo.R

class OutSideViewGroup : LinearLayout {
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        Log.d("OutSideViewGroup", "${findName()} onLayout==>>l=$l; t=$t; r=$r; b=$b")
        super.onLayout(changed, l, t, r, b)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.d("OutSideViewGroup", "${findName()} onTouchEvent ${event?.action} vg==>>")
        var b = super.onTouchEvent(event)
        Log.d("OutSideViewGroup", "${findName()} onTouchEvent ${event?.action} ==>>b=$b")
        return b
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        Log.d("OutSideViewGroup", "${findName()} dispatchTouchEvent ${ev?.action} vg==>>")
        var b = super.dispatchTouchEvent(ev)
        Log.d("OutSideViewGroup", "${findName()} dispatchTouchEvent ${ev?.action} ==>>b=$b")
        return b
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        Log.d("OutSideViewGroup", "${findName()} onInterceptTouchEvent ${ev?.action} vg==>>")
        var b = super.onInterceptTouchEvent(ev)
        Log.d("OutSideViewGroup", "${findName()} onInterceptTouchEvent ${ev?.action} ==>>b=$b")
        return b
    }

    private fun findName(): String {
        return when(id){
            R.id.viewGroupRoot -> "viewGroupRoot"
            R.id.viewGroupOut -> "viewGroupOut"
            else -> "${id}"
        }
    }
}